package com.eian.boot.rabbit.util;

import com.eian.boot.common.id.IdHelper;
import org.slf4j.MDC;

public class TraceIdUtil {
    private static final String TRACE_ID_KEY = "traceId";

    public static String getTraceId() {
        String traceId = MDC.get(TRACE_ID_KEY);
        if (traceId == null || traceId.isEmpty()) {
            return generateTraceId();
        }
        return traceId;
    }

    public static void setTraceId(String traceId) {
        if (traceId != null && !traceId.isEmpty()) {
            MDC.put(TRACE_ID_KEY, traceId);
        }
    }

    private static String generateTraceId() {
        return IdHelper.simpleUuid();
    }

    public static void clearTraceId() {
        MDC.remove(TRACE_ID_KEY);
    }
}